package com.newdriver.edu.inf.manager.dao;

import com.newdriver.edu.inf.manager.domain.Student;

//库管
public class Dao {
    private static Student[] stus = new Student[5];
    public boolean addStuddent(Student stu) {

        //创建数组
        //定义index
        int index = -1;
        //遍历 判断是否为null
        for (int i = 0; i < stus.length; i++) {
            Student student = stus[i];
            if (student == null) {
                index = i;
                break;
            }
        }
        //返回是否添加成功  boolean
        if (index == -1) {
            return false;
        }else{
            stus[index]=stu;
            return true;
        }
    }
    public Student[] findAllStudent() {
        return stus;
    }

    public void delStuById(String delId) {
        //查找id索引
        int index = getIndex(delId);
        //使用null覆盖
        stus[index]=null;
    }
    public int getIndex (String id){
        int index=-1;
        for (int i = 0; i < stus.length; i++) {
            Student stu = Dao.stus[i];
            if (stu != null && stu.getId().equals(id)) {
                index=i;
                break;
            }
        }
        return index;
    }


    public void updateStudent(String updateId, Student newStudent) {
        int index = getIndex(updateId);
        stus[index]=newStudent;
        }
}

